package com.itboye.eweilisten.me.zhanghu;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.app.MyApplication;
import com.itboye.eweilisten.base.BaseActivity;
import com.itboye.eweilisten.me.login.LoginActivity;
import com.itboye.eweilisten.presenter.UserPresenter;
import com.itboye.eweilisten.util.loadingutil.MAlert;
import com.itboye.eweilisten.util.popwindow.UIAlertView;
import com.itboye.eweilisten.util.sputil.Const;
import com.itboye.eweilisten.util.sputil.SPUtils;
import com.itboye.eweilisten.util.widget.IsStringSPUtils;
import com.itboye.eweilisten.volley.ResultEntity;

import java.util.Observable;
import java.util.Observer;

/**
 * Created by itboye on 2017/3/23.
 */

public class UpdatePhoneActivity extends BaseActivity implements Observer, UIAlertView.ClickListenerInterface {
    private TextView tvUpdatePhone;//账号
    private EditText tvPhone;//原手机号
    private EditText tvNewPhone;//新手机号
    private EditText tvConmintPhone;//确认
    private Button update_phone_ok;
    private ImageView back;
    private TextView tvTitle;
    private UserPresenter presenter;
    UIAlertView alertView;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_update_phone);
        init();
    }

    public void init() {
        presenter = new UserPresenter(this);
        tvTitle.setText("更换手机号");
        String userNmae = (String) SPUtils.get(MyApplication.instance, null,
                Const.EMAIL, "");

        if(!TextUtils.isEmpty(userNmae) && userNmae.length() > 6 ){
            StringBuilder sb  =new StringBuilder();
            for (int i = 0; i < userNmae.length(); i++) {
                char c = userNmae.charAt(i);
                if (i >= 3 && i <= 6) {
                    sb.append('*');
                } else {
                    sb.append(c);
                }
            }
            tvUpdatePhone.setText(sb.toString());
        }

    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.update_phone_ok:
                String oldPhone = tvPhone.getText().toString();
                String newsPhone = tvNewPhone.getText().toString();
                String okNewsPhone = tvConmintPhone.getText().toString();
                String mobile = (String) SPUtils.get(MyApplication.instance, null,
                        Const.MOBILE, "");
                if (oldPhone.equals("")) {
                    MAlert.alert("原手机号不能为空");
                    return;
                }
                if (newsPhone.equals("") || newsPhone == null) {
                    MAlert.alert("请输入新手机号");
                    return;
                }
                if (oldPhone.length() != 11) {
                    MAlert.alert("手机格式不正确!");
                    return;
                }
                if (newsPhone.length() != 11) {
                    MAlert.alert("手机格式不正确!");
                    return;
                }
                if (okNewsPhone.length() != 11) {
                    MAlert.alert("手机格式不正确!");
                    return;
                }
                if (!oldPhone.equals(mobile)) {
//                    MAlert.alert("输入的原手机号有误\n请重新输入");
//                    alertView = new UIAlertView(this, "温馨提示！", "输入的原手机号有误\n" + "请重新输入", "好", "", type);
//                    alertView.show();
//                    alertView.setClicklistener(this);
                    MAlert.alert("原手机号不正确");
                    return;
                }
                if (!okNewsPhone.equals(newsPhone)) {
                    MAlert.alert("您输入的号码不一致");
                    return;
                } else {
                    showProgressDialog("更新中...", true);
                    presenter.onUpdatePhone(IsStringSPUtils.getSid(), IsStringSPUtils.IsUid(), oldPhone, newsPhone);
                }
                break;

        }
    }

    @Override
    public void update(Observable o, Object arg) {
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
        ResultEntity entity = handlerError(arg);
        if (entity != null) {
            if(entity.getCode().equals("1111")){
                Intent intent=new Intent(MyApplication.instance, LoginActivity.class);
                startActivity(intent);
                return;
            }
            if (entity.getEventType() == presenter.UpdatePhone_success) {
                MAlert.alert("" + entity.getData());
                finish();
                SPUtils.put(UpdatePhoneActivity.this, null,
                        Const.MOBILE, tvConmintPhone.getText().toString().trim());
            }
            if (entity.getEventType() == presenter.UpdatePhone_fail) {
                MAlert.alert("" + entity.getData());
            }
        }

    }

    @Override
    public void doLeft() {
        alertView.dismiss();
    }

    @Override
    public void doRight() {

    }
}
